using System.Collections;
using System.Collections.Generic;
using BCW.Friends;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ButtonFriends : MonoBehaviour
{
	private UIButton thisButton;

	private int countNotification;

	[SerializeField]
	private UILabel lbNotification;

	private void OnEnable()
	{
		UpdateNotification();
		FriendsController.OnReciveNewFriends += UpdateNotification;
		FriendsController.OnReciveNewInvites += UpdateNotification;
		FriendsController.OnReciveNewMessages += UpdateNotification;
	}

	private void OnDisable()
	{
		FriendsController.OnReciveNewFriends -= UpdateNotification;
		FriendsController.OnReciveNewInvites -= UpdateNotification;
		FriendsController.OnReciveNewMessages -= UpdateNotification;
	}

	private void OnClick()
	{
		CoroutineRunner.Instance.StartCoroutine(LoadingFriendController());
	}

	private IEnumerator LoadingFriendController()
	{
		if (FriendsUIController.instance == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			yield return SceneManager.LoadSceneAsync("Lods_Level_3", LoadSceneMode.Additive);
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		yield return null;
		FriendsUIController.instance.Show();
	}

	private void UpdateNotification(Dictionary<string, uint> dictMsg)
	{
		UpdateNotification();
	}

	private void UpdateNotification()
	{
		if (FriendsController.instance != null)
		{
			countNotification = (int)FriendsController.instance.CountNewFriends + FriendsController.instance.InvitesIds.Count + (int)FriendsController.instance.CountNewMessages;
			if (countNotification > 0)
			{
				lbNotification.text = countNotification.ToString();
				lbNotification.gameObject.SetActive(true);
			}
			else
			{
				lbNotification.gameObject.SetActive(false);
			}
		}
	}
}
